package net.yebaihe.buildapp;

import com.waps.AppConnect;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class BuildappActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppConnect.getInstance(this);
        setContentView(R.layout.main);
        
        initDataDB();
        
        Button btnCreate=(Button) this.findViewById(R.id.idcreate);
        btnCreate.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				startActivity(new Intent (BuildappActivity.this, ChooseTemplate.class));
			}
        });
        
        Button btnMaintain=(Button) this.findViewById(R.id.idmaintain);
        btnMaintain.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				startActivity(new Intent (BuildappActivity.this, MaintainApp.class));
			}
        });
    }

	private void initDataDB() {
		Object[][] tempdata={
				{1,"单本电子书","http://lily.newim.net/aupages/ggjs/bookggjs.apk","单本电子书，由章节目录和各章内容组成。","sbook.png"},	
		};
		
		Object[][] tempparamdata={
				{1,1,1,"启动画面","1:png:480:800","程序启动时显示的画面,比如书的封面，480x800，png图片"},	
				{2,1,2,"图书背景","1:png:480:800","阅读图书时衬托文字的背景画面，480x800，png图片"},	
				{3,1,3,"目录按钮","1:png:290:69","目录标题的背景画面，290x69，png图片"},	
				{4,1,4,"小节文本","2:txt","每个小节的文本，GBK编码的txt格式"},	
		};
		
		
		MyHelper helper=MyHelper.get(this);
		SQLiteDatabase db = helper.getWritableDatabase();

		for (int i=0;i<tempdata.length;i++){
			Cursor cursor = db.rawQuery("select * from template where _id="+tempdata[i][0],null);
			if (!cursor.moveToFirst()){
				db.execSQL("insert into template values ("+tempdata[i][0]
				                                          +",'"+tempdata[i][1]
				                                          +"','"+tempdata[i][2]
				                                          +"','"+tempdata[i][3]
				                                          +"','"+tempdata[i][4]
				                                          +"')");
			}
			cursor.close();
		}
		for (int i=0;i<tempparamdata.length;i++){
			Cursor cursor = db.rawQuery("select * from tparams where _id="+tempparamdata[i][0],null);
			if (!cursor.moveToFirst()){
				db.execSQL("insert into tparams values ("+tempparamdata[i][0]
				       				                      +","+tempparamdata[i][1]
				       				    				  +","+tempparamdata[i][2]                    
				                                          +",'"+tempparamdata[i][3]
				                                          +"','"+tempparamdata[i][4]
				                                          +"','"+tempparamdata[i][5]
				                                          +"')");
			}
			cursor.close();
		}
		
		db.close();
	}
	
	@Override
	public void onBackPressed(){
		finish();
		AppConnect.getInstance(this).finalize();
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.clear();
		menu.add(Menu.NONE, Menu.FIRST + 2, 2, "更多应用").setIcon(R.drawable.gd);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case Menu.FIRST + 2:
			showAppWall();
			break;
		}
		return true;
	}
    protected void showAppWall(){
		AppConnect.getInstance(this).showOffers(this);
    }

	
}